/**
 * Created by sunyxq on 16/3/26.
 */
angular.module('hyt.manageYunDanService',[])

    .factory('CydChangeFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz'),result,shfs,fkfs,ddz,count,spydresult,countYd,setYd,
            cydChangeDetail = {};
        var resource = $resource(api);
        var pagesize=10;
        var bz={};

        return{
            //判断承运单号是否重复
            checkCydhExist:function(sid,user_id,dw_id,yh_id,cydh,cyd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :'sqlKeyBiz.list#count_cyd_by_cydh',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'cydh':cydh,
                        'cyd_id':cyd_id
                    }
                },function(resp){
                    count=resp;
                    console.log(resp);
                    $rootScope.$broadcast('checkCydh');
                })
            },
            getCount:function(){
                return count;
            },

            getChangeDetail : function(id,sid,user_id,yh_id){
                return resource.save({
                    "ACTION_NAME" : "sqlKeyBiz.fetch#yundan_fetch",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        "id" : id,
                        "yh_id":yh_id
                    }
                },function(r){
                    console.log(r);

                    if(r.ACTION_RETURN_CODE == "000000") {

                        cydChangeDetail = r.ACTION_INFO;

                        $rootScope.$broadcast('CydDetail.changeUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },

            getChangeDetailContent : function(){
                if(cydChangeDetail == undefined){
                    return false;
                }
                return cydChangeDetail;
            },

            //初始化送货方式
            queryShfs : function(sid,yh_id){
                return resource.save({
                    "SID":sid,
                    "USER_ID":'11111111',
                    "ACTION_NAME" : 'sqlKeyBiz.list#y_param_set_query',
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        'type':'shfs'
                    }
                },function(r){
                    console.log(r);
                    shfs = r.ACTION_INFO.RESULT_LIST;
                    $rootScope.$broadcast('CydChange.changeUpdated');

                });
            },
            getShfs:function(){
                return shfs;
            },

            //初始化付款方式
            queryFkfs : function(sid,yh_id){
                return resource.save({
                    "SID":sid,
                    "USER_ID":'11111111',
                    "ACTION_NAME" :  'sqlKeyBiz.list#y_param_set_query',
                    "ACTION_INFO" : {
                        'type':'fkfs',
                        'yh_id':yh_id
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        console.log(r);
                        fkfs = r.ACTION_INFO.RESULT_LIST;
                        $rootScope.$broadcast('CydChange.changeUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            getFkfs:function(){
                return fkfs;
            },

            //初始化到达站
            queryDdz:function(user_id,sid,yh_id,dw_id,zd_id){
                return resource.save({
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_NAME" :  'sqlKeyBiz.list#bmxx_query_by_zd_id',
                    "ACTION_INFO" : {
                        'dw_id':dw_id,
                        'zd_id':zd_id,
                        'yh_id':yh_id
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        console.log(r);
                        ddz = r.ACTION_INFO.RESULT_LIST;
                        $rootScope.$broadcast('CydChange.changeUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            getDdz:function(){
                return ddz;
            },

            //初始化包装
            queryBz:function(sid,dw_id,yh_id,user_id,bzmc){
                var hasNextPage =true;
                return resource.save({
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_NAME" :  'sqlKeyBiz.page#baozhuang_list',
                    "ACTION_INFO" : {
                        'dw_id':dw_id,
                        'pagesize':'10',
                        'current_page':'1',
                        'yh_id':yh_id,
                        'mc':bzmc
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        console.log(r);
                        bz[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('CydChange.changeUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            getBzList : function(){
                if(bz[pagesize] == undefined){
                    return false;
                }
                return bz[pagesize];
            },
            getMoreBz : function(sid,dw_id,yh_id,user_id){
                if(bz[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = bz[pagesize].hasNextPage;
                var current_page = bz[pagesize].current_page;
                var moreBzListData = bz[pagesize].data;
                resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.page#baozhuang_list',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'yh_id':yh_id
                    }
                },function(r){
                    current_page++;
                    if(r.ACTION_RETURN_CODE == "000000") {
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }

                        moreBzListData = moreBzListData.concat(r.ACTION_INFO.RESULT_LIST);

                        bz[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreBzListData
                        };
                        $rootScope.$broadcast('CydChange.changeUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            hasNextPage: function() {
                if (bz[pagesize] === undefined) {
                    return false;
                }
                return bz[pagesize].hasNextPage;
            },
            //保存
            saveCyd : function(dw_id,cyd,yh_id,sid,user_id,zd_id,cyrq,bmmc,zsxm){
                return resource.save({
                    "SID":sid,
                    "USER_ID":user_id,
                    'ACTION_NAME' : 'sqlKeyBiz.transUpdate#updateCyd',
                    'ACTION_INFO' : {
                        'yhmc':zsxm,
                        'cyd_id':cyd.cyd_id,
                        'cyrq':cyrq,
                        'dw_id': dw_id,
                        'cydh': cyd.cydh,
                        'yh_id': yh_id,
                        'bm_id': zd_id,
                        'bmmc':bmmc,
                        'lrbmmc':bmmc,
                        'shbm_id':cyd.shbm_id,
                        'shbmmc': cyd.shbmmc,
                        'cylx': cyd.cylx,
                        'code': cyd.shfs,
                        'shrmc': cyd.shrmc,
                        'shrdh': cyd.shrdh,
                        'shrdz': cyd.shrdz,
                        'fhrmc': cyd.fhrmc,
                        'fhrdh': cyd.fhrdh,
                        'fhrdz': cyd.fhrdz,
                        'mdd': cyd.mdd,
                        'hwmc': cyd.hwmc,
                        'sl': cyd.sl,
                        'zl': cyd.zl,
                        'tj': cyd.tj,
                        'bzlx': cyd.bzlx,
                        'bz': cyd.bz,
                        'yf' : cyd.yf,
                        'yf_xf': cyd.yf_xf,
                        'yf_hf': cyd.yf_hf,
                        'yf_yj': cyd.yf_yj,
                        'yf_tf': cyd.yf_tf,
                        'yf_ycdk': cyd.yf_ycdk,
                        'xxf_xf': cyd.xxf_xf,
                        'xxf_qf': cyd.xxf_qf,
                        'dsk_hf': cyd.dsk_hf,
                        'shf1':cyd.shf1,
                        'shfs': cyd.shfs,
                        'thf': cyd.thf,
                        'be': cyd.be,
                        'bf': cyd.bf,
                        'tbzt':cyd.tbzt,
                        'yfdfzt':cyd.yfdfzt,
                        'dskdfzt':cyd.dskdfzt,
                        'yf_df': cyd.yf_df,
                        'dsk_xf': cyd.dsk_xf,
                        'qdfs':cyd.qdfs,
                        'sfyqd':cyd.sfyqd
                    }
                },function(r){
                    result=r;
                    console.log(r);
                    $rootScope.$broadcast('CydSaveChange.saveChangeUpdated');
                })
            },
            getResult: function(){
                return result;
            },
            ydsaveAndPrint : function(dw_id,cyd,yh_id,sid,user_id,zd_id,cyrq,bmmc,zsxm){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME : 'sqlKeyBiz.transUpdate#updateCyd',
                    ACTION_INFO : {
                        'yhmc':zsxm,
                        'cyd_id':cyd.cyd_id,
                        'cyrq':cyrq,
                        'dw_id': dw_id,
                        'cydh': cyd.cydh,
                        'yh_id': yh_id,
                        'bm_id': zd_id,
                        'bmmc':bmmc,
                        'lrbmmc':bmmc,
                        'shbm_id':cyd.shbm_id,
                        'shbmmc': cyd.shbmmc,
                        'cylx': cyd.cylx,
                        'code': cyd.shfs,
                        'shrmc': cyd.shrmc,
                        'shrdh': cyd.shrdh,
                        'shrdz': cyd.shrdz,
                        'fhrmc': cyd.fhrmc,
                        'fhrdh': cyd.fhrdh,
                        'fhrdz': cyd.fhrdz,
                        'mdd': cyd.mdd,
                        'hwmc': cyd.hwmc,
                        'sl': cyd.sl,
                        'zl': cyd.zl,
                        'tj': cyd.tj,
                        'bzlx': cyd.bzlx,
                        'bz': cyd.bz,
                        'yf' : cyd.yf,
                        'yf_xf': cyd.yf_xf,
                        'yf_hf': cyd.yf_hf,
                        'yf_yj': cyd.yf_yj,
                        'yf_tf': cyd.yf_tf,
                        'yf_ycdk': cyd.yf_ycdk,
                        'xxf_xf': cyd.xxf_xf,
                        'xxf_qf': cyd.xxf_qf,
                        'dsk_hf': cyd.dsk_hf,
                        'shf1':cyd.shf1,
                        'shfs': cyd.shfs,
                        'thf': cyd.thf,
                        'be': cyd.be,
                        'bf': cyd.bf,
                        'tbzt':cyd.tbzt,
                        'yfdfzt':cyd.yfdfzt,
                        'dskdfzt':cyd.dskdfzt,
                        'yf_df': cyd.yf_df,
                        'dsk_xf': cyd.dsk_xf,
                        'qdfs':cyd.qdfs,
                        'sfyqd':cyd.sfyqd
                    }
                },function(r){
                    spydresult=r;
                    console.log(r);
                    $rootScope.$broadcast('YdSaveAndPrintCyd.CydUpdated');
                })
            },
            getSpydResult: function(){
                return spydresult;
            },
            checkCydhExistYd:function(sid,user_id,dw_id,yh_id,cydh,cyd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :'sqlKeyBiz.list#count_cyd_by_cydh',
                    ACTION_INFO : {
                        'dw_id':dw_id,
                        'yh_id':yh_id,
                        'cydh':cydh,
                        'cyd_id':cyd_id
                    }
                },function(resp){
                    countYd=resp;
                    console.log(resp);
                    $rootScope.$broadcast('checkCydhYd');
                })
            },
            getCountYd:function(){
                return countYd;
            },
            fetchPrintYd:function(sid,user_id,yh_id,zd_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME:'sqlKeyBiz.dyFetch#dyFetch',
                    ACTION_INFO:{
                        'yh_id':yh_id,
                        'zd_id':zd_id
                    }
                },function(resp){
                    setYd=resp;
                    console.log(resp);
                    $rootScope.$broadcast('QueryYd.PrintSet');
                });
            },
            getSetYd:function(){
                return setYd;
            }
        }
    })

    //获取承运单详情数据
    .factory('CydDetailFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz'),
            cydDetailData = {};

        var resource = $resource(api);

        return {
            //获取承运单详情
            getDetail : function(id,sid,user_id,yh_id){

                return resource.save({
                    "ACTION_NAME" : "sqlKeyBiz.fetch#yundan_fetch",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        "id" : id
                    }
                }, function (r) {
                    if(r.ACTION_RETURN_CODE == "000000") {

                        cydDetailData = r.ACTION_INFO;

                        $rootScope.$broadcast('CydDetail.detailUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });

            },

            getDetailContent : function(){
                if(cydDetailData == undefined){
                    return false;
                }
                return cydDetailData;
            },

        }
    })

    //获取承运单详情进程数据
    .factory('CydProcessFactory',function($resource,$rootScope,ENV,Storage){
        var api =  Storage.get('fwqdz'),
            cydProcessData = {};

        var resource = $resource(api);

        return{
            //获取承运单进程
            getProcess : function(id,sid,user_id,yh_id){
                return resource.save({
                    "ACTION_NAME" : "sqlKeyBiz.page#jincheng_query",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        "id" : id
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        console.log(r);
                        cydProcessData = r.ACTION_INFO.RESULT_LIST;

                        $rootScope.$broadcast('CydProcess.processUpdated');
                    }
                });
            },

            getProcessContent : function(){
                if(cydProcessData == undefined){
                    return false;
                }
                return cydProcessData;
            }
        }
    })

//获取承运单列表数据
.factory('CydListFactory',function($resource, $rootScope,ENV,Storage,$state,$ionicLoading){
    var APIUrl =  Storage.get('fwqdz'),
        cydListData = {},cydListData1={},
        cydhj={},cydhjmenu={},
        pagesize = 10;

    var resource = $resource(APIUrl);

    return {
        //获取承运单列表数据
        getCydList: function(sid,user_id,yh_id,zd_id,dw_id,cydh,startDate,endDate,fhrmc,fhrdh,shrdh,cyzt,shbm_id) {
            var hasNextPage =true;    //判断是否有下一页

            return resource.save({
                'ACTION_NAME': 'sqlKeyBiz.page#yundan_query',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'cydh':cydh,
                    'yh_id':yh_id,
                    'zd_id':zd_id,
                    'dw_id':dw_id,
                    'pagesize':'10',
                    'current_page':'1',
                    'djsj_ks':startDate,
                    'djsj_js':endDate,
                    'fhrmc':fhrmc,
                    'fhrdh':fhrdh,
                    'shrdh':shrdh,
                    'cyzt':cyzt,
                    'shbm_id':shbm_id
                }
            }, function(r) {
                if(r.ACTION_RETURN_CODE == "000000"){
                    if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                        hasNextPage = false;
                    }

                    cydListData[pagesize] = {
                        hasNextPage : hasNextPage,
                        'current_page' : 2,
                        'data' : r.ACTION_INFO.RESULT_LIST,
                        'cydzsl': r.ACTION_INFO.TOTAL
                    };

                    $rootScope.$broadcast('CydList.cydUpdated');
                }else if (r.ACTION_RETURN_CODE == "000005"){
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "您的账号已在其他设备登录",
                        duration: 1500
                    });
                    $state.go("login");
                }


            });

        },

        //获取承运单列表单页数据
        getCyd : function(){
            if(cydListData[pagesize] == undefined){
                return false;
            }
            return cydListData[pagesize];
        },

        //获取承运单列表更多页数据
        getMoreCyd:function(sid,user_id,yh_id,zd_id,dw_id,cydh,startDate,endDate,fhrmc,fhrdh,shrdh,cyzt,shbm_id){

            //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
            if(cydListData[pagesize] == undefined){
                return false;
            }

            //获取以前的数据
            var hasNextPage = cydListData[pagesize].hasNextPage;
            var current_page = cydListData[pagesize].current_page;
            var moreCydListData = cydListData[pagesize].data;

            //console.log(moreCydListData);

            resource.save({
                'ACTION_NAME': 'sqlKeyBiz.page#yundan_query',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'pagesize':pagesize,
                    'current_page':current_page,
                    'cydh':cydh,
                    'fhrdh':fhrdh,
                    'shrdh':shrdh,
                    'djsj_js':endDate,
                    'djsj_ks':startDate,
                    'yh_id':yh_id,
                    'zd_id':zd_id,
                    'dw_id':dw_id,
                    'fhrmc':fhrmc,
                    'cyzt':cyzt,
                    'shbm_id':shbm_id
                }
            }, function (r) {

                current_page++;

                if(r.ACTION_RETURN_CODE == "000000"){
                    if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                        hasNextPage = false;
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }

                    moreCydListData = moreCydListData.concat(r.ACTION_INFO.RESULT_LIST);

                    cydListData[pagesize] = {
                        hasNextPage : hasNextPage,
                        'current_page' : current_page,
                        'data' : moreCydListData
                    };

                    $rootScope.$broadcast('CydList.cydUpdated');
                }


            })
        },

        hasNextPage: function() {
            if (cydListData[pagesize] === undefined) {
                return false;
            }
            return cydListData[pagesize].hasNextPage;
        },
        //菜单搜索运单
        getCydList1: function(sid,user_id,yh_id,zd_id,dw_id,cydh,startDate,endDate,mc,fhrdh,shrdh,cyzt,shbm_id) {
            var hasNextPage =true;    //判断是否有下一页

            return resource.save({
                'ACTION_NAME': 'sqlKeyBiz.page#yundanmenulist',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'cydh':cydh,
                    'yh_id':yh_id,
                    'zd_id':zd_id,
                    'dw_id':dw_id,
                    'pagesize':'10',
                    'current_page':'1',
                    'djsj_ks':startDate,
                    'djsj_js':endDate,
                    'mc':mc,
                    'fhrdh':fhrdh,
                    'shrdh':shrdh,
                    'cyzt':cyzt,
                    'shbm_id':shbm_id
                }
            }, function(r) {
                if(r.ACTION_RETURN_CODE == "000000"){
                    if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                        hasNextPage = false;
                    }
                    cydListData1[pagesize] = {
                        hasNextPage : hasNextPage,
                        'current_page' : 2,
                        'data' : r.ACTION_INFO.RESULT_LIST,
                        'cydzsl': r.ACTION_INFO.TOTAL
                    };

                    $rootScope.$broadcast('CydList1.cydUpdated');
                }else if (r.ACTION_RETURN_CODE == "000005"){
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "您的账号已在其他设备登录",
                        duration: 1500
                    });
                    $state.go("login");
                }
            });

        },

        //获取承运单列表单页数据
        getCyd1 : function(){
            if(cydListData1[pagesize] == undefined){
                return false;
            }
            return cydListData1[pagesize];
        },

        //获取承运单列表更多页数据
        getMoreCyd1:function(sid,user_id,yh_id,zd_id,dw_id,cydh,startDate,endDate,mc,fhrdh,shrdh,cyzt,shbm_id){

            //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
            if(cydListData1[pagesize] == undefined){
                return false;
            }

            //获取以前的数据
            var hasNextPage = cydListData1[pagesize].hasNextPage;
            var current_page = cydListData1[pagesize].current_page;
            var moreCydListData1 = cydListData1[pagesize].data;

            //console.log(moreCydListData);

            resource.save({
                'ACTION_NAME': 'sqlKeyBiz.page#yundanmenulist',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'pagesize':pagesize,
                    'current_page':current_page,
                    'cydh':cydh,
                    'fhrdh':fhrdh,
                    'shrdh':shrdh,
                    'djsj_js':endDate,
                    'djsj_ks':startDate,
                    'yh_id':yh_id,
                    'zd_id':zd_id,
                    'dw_id':dw_id,
                    'mc':mc,
                    'cyzt':cyzt,
                    'shbm_id':shbm_id
                }
            }, function (r) {

                current_page++;

                if(r.ACTION_RETURN_CODE == "000000"){
                    if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                        hasNextPage = false;
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }

                    moreCydListData1 = moreCydListData1.concat(r.ACTION_INFO.RESULT_LIST);

                    cydListData1[pagesize] = {
                        hasNextPage : hasNextPage,
                        'current_page' : current_page,
                        'data' : moreCydListData1
                    };

                    $rootScope.$broadcast('CydList1.cydUpdated');
                }


            })
        },

        hasNextPage1: function() {
            if (cydListData1[pagesize] === undefined) {
                return false;
            }
            return cydListData1[pagesize].hasNextPage;
        },
        getCydhj:function(sid,user_id,yh_id,zd_id,dw_id,cydh,startDate,endDate,fhrmc,fhrdh,shrdh,cyzt,shbm_id){
            return resource.save({
                'ACTION_NAME': 'sqlKeyBiz.list#yundanhj_query',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'cydh':cydh,
                    'yh_id':yh_id,
                    'zd_id':zd_id,
                    'dw_id':dw_id,
                    'djsj_ks':startDate,
                    'djsj_js':endDate,
                    'fhrmc':fhrmc,
                    'fhrdh':fhrdh,
                    'shrdh':shrdh,
                    'cyzt':cyzt,
                    'shbm_id':shbm_id
                }
            }, function(r) {
                if(r.ACTION_RETURN_CODE == "000000"){
                    cydhj= r.ACTION_INFO.RESULT_LIST;
                }else if (r.ACTION_RETURN_CODE == "000005"){
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "您的账号已在其他设备登录",
                        duration: 1500
                    });
                    $state.go("login");
                }
                $rootScope.$broadcast('CydHj.cydUpdated');

            });
        },
        getResult:function(){
            return cydhj[0];
        },
        getCydhjmenu:function(sid,user_id,yh_id,zd_id,dw_id,cydh,mc){
            return resource.save({
                'ACTION_NAME': 'sqlKeyBiz.list#yundanhj_menu',
                'SID':sid,
                'USER_ID':user_id,
                'ACTION_INFO': {
                    'cydh':cydh,
                    'yh_id':yh_id,
                    'zd_id':zd_id,
                    'dw_id':dw_id,
                    'mc':mc
                }
            }, function(r) {
                if(r.ACTION_RETURN_CODE == "000000"){
                    cydhjmenu= r.ACTION_INFO.RESULT_LIST;
                }else if (r.ACTION_RETURN_CODE == "000005"){
                    $ionicLoading.show({
                        noBackdrop: true,
                        template: "您的账号已在其他设备登录",
                        duration: 1500
                    });
                    $state.go("login");
                }
                $rootScope.$broadcast('CydHjmenu.cydUpdated');

            });
        },
        getResultmenu:function(){
            return cydhjmenu[0];
        }
    };
})
//获取经纬度
.factory('GPSFactory',function($resource,$rootScope,ENV,Storage){
    var api =  Storage.get('fwqdz'),
        GPSLocation;

    var resource = $resource(api);

    return{
        getGPS : function(id,sid,user_id,yh_id,dw_id){
            return resource.save({
                "ACTION_NAME" : "sqlKeyBiz.fetch#location_query",
                "SID":sid,
                "USER_ID":user_id,
                "ACTION_INFO" : {
                    'yh_id':yh_id,
                    'state':1,
                    "cyd_id" : id,
                    'dw_id':dw_id
                }
            },function(r){
                console.log(r);
                GPSLocation = r.ACTION_INFO;
                $rootScope.$broadcast('GPS.GPSUpdated');
            });
        },

        getResult : function(){
            return GPSLocation;
        }
    }
});
